//
//  XUOuthModel.m
//  新浪微博
//
//  Created by xuwenxin on 15-4-12.
//  Copyright (c) 2015年 xuwenxin. All rights reserved.
//

#import "XUOuthModel.h"

@implementation XUOuthModel


- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+ (instancetype)outhWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
// 归档的时候调用
- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.access_token forKey:@"access_token"];
    [encoder encodeObject:self.expires_in forKey:@"expires_in"];
    [encoder encodeObject:self.remind_in forKey:@"remind_in"];
    [encoder encodeObject:self.uid forKey:@"uid"];
    [encoder encodeObject:self.expires_time forKey:@"expires_time"];
    [encoder encodeObject:self.profile_image_url forKey:@"profile_image_url"];
}
// 解档的时候调用
- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        self.access_token = [decoder decodeObjectForKey:@"access_token"];
        self.expires_in = [decoder decodeObjectForKey:@"expires_in"];
        self.remind_in = [decoder decodeObjectForKey:@"remind_in"];
        self.uid = [decoder decodeObjectForKey:@"uid"];
        self.expires_time = [decoder decodeObjectForKey:@"expires_time"];
        self.profile_image_url = [decoder decodeObjectForKey:@"profile_image_url"];
    }
    return self;
}

// 存储当前授权模型
- (BOOL)save{
    // 1.获取存储路径
    NSString *filePath = [@"account.data" appendDocumentDir];
    // 2存储数据
    return  [NSKeyedArchiver archiveRootObject:self toFile:filePath];
}
// 读取当前授权模型
+ (instancetype)accountFromSandbox{
    // 1.获取存储路径
    NSString *filePath = [@"account.data" appendDocumentDir];
    // 2.解归档授权信息
    XUOuthModel *account = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    // 3.判断授权是否过期
    // 3.1获取当前的时间
//    NSDate *now = [NSDate date];
//    if ([now compare:account.expires_time] != NSOrderedAscending) {
//        return nil;
//    }
    return account;
}



@end
